<template>
  <div class="zuida">
    <div class="dl">
      <div class="text">
        <h2>注册</h2>
      </div>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-input type="name" prefix-icon="el-icon-user" v-model="ruleForm.name" placeholder="请输入用户名"></el-input>
  <el-input type="password" prefix-icon="el-icon-lock" v-model="ruleForm.password" show-password placeholder="请设置密码"></el-input>
  <el-input type="password" prefix-icon="el-icon-lock" v-model="ruleForm.repassword" show-password placeholder="请确认密码"></el-input>
  
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
<div class="zhuce">
  已有账号?点击 <a href="/">登录</a>
</div>
    </div>
    
  </div>
</template>
<script>
  export default {
     data() {
      return {
        ruleForm: {
          name: '',
          password: '',
          repassword: ''
        },
        formdata:{
          name:'',
          password:''
        },
      };
    },
    mounted(){
      this.$store.commit('noShow')
    },
    methods: {
      request(){
        if(this.ruleForm.password!=this.ruleForm.repassword){
          this.$message({
          showClose: true,
          message: '抱歉，两次密码不同!请检查后输入!',
          type: 'warning'
        })
        
      }else{
        this.formdata.name=this.ruleForm.name
        this.formdata.password=this.ruleForm.password
        this.axios.post("/zhuce",this.formdata
        ).then((res)=>{
          this.$message({
          showClose: true,
          message: '恭喜你，注册成功!',
          type: 'success'
        })
        this.token=res.data.data;
        localStorage.setItem('token',res.data.data)
      }).catch((err)=>{
        this.$message({
          showClose: true,
          message: '抱歉!注册失败!',
          type: 'warning'
        });
      })
      }
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.request();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        // this.$refs[formName].resetFields();
        this.ruleForm.name=''
        this.ruleForm.password=''
      }
    }
  }
</script>
<style lang="less" scoped>
.zuida{
  width: 100%;
  height: 98vh;
  background-color: skyblue;
  display: flex;
  // flex-direction:column;
  justify-content:center;
  position: relative;
}
.dl{
  background-color: aqua;
  position: absolute;
    width: 370px;
    height: 270px;
    top:38vh;
    border-radius: 16px;
    box-shadow: 2px 2px 10px #7e7e7e;
  }
  .text{
    width: 100%;
    height: 40px;
    text-align: center;
  }
  .zhuce{
    text-align: center;
    margin-top: -16px;
    width: 100%;
    height: 30px;
  }
  /deep/ .el-form{
    width: 80%;
    margin-left: 10%;
  }
</style> 